package com.cusc.module_login.mvvm.vm;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.cusc.lib_api.bean.login.request.Req_login;
import com.cusc.lib_api.bean.login.response.LoginInfoBean;
import com.cusc.lib_api.bean.login.response.LoginResponse;
import com.cusc.module_login.R;
import com.cusc.module_login.mvvm.model.LoginNetRegister;
import com.tcgroup.phone.base_core.event.event_livedata.SingleLiveEvent;
import com.tcgroup.phone.base_core.framework.mvvm.viewmodel.BaseViewModel;
import com.tcgroup.phone.base_core.store.MmkvStore;
import com.tcgroup.phone.base_core.store.StoreKey;
import com.tcgroup.phone.base_core.utils.MD5Util;
import com.tcgroup.phone.base_core.utils.ToastUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/19.
 * 包名: com.cusc.module_login.vm
 * 类名: OSAndroidMvvm
 */
public class LoginViewModel extends BaseViewModel<LoginNetRegister>{

    public ObservableField<String> userName = new ObservableField<>();
    public ObservableField<String> userPassWord = new ObservableField<>();
    public ObservableField<Boolean> isRememberPwAndName = new ObservableField<>();
    protected SingleLiveEvent<Void> goPlatformViewEvent;
    protected LoginInfoBean mLoginInfoBean;

    public LoginViewModel(@NonNull Application application, LoginNetRegister netRegister) {
        super(application, netRegister);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        isRememberPwAndName.set(MmkvStore.getInstance().getMmkv().decodeBool(StoreKey.Module_Login.Save_Name_Password,false));
        userName.set(MmkvStore.getInstance().getMmkv().decodeString(StoreKey.Module_Login.User_Name,""));
        userPassWord.set(MmkvStore.getInstance().getMmkv().decodeString(StoreKey.Module_Login.Pass_Word,""));
    }

    public SingleLiveEvent<Void> getGoPlatformEvent() {
        return goPlatformViewEvent = createLiveData(goPlatformViewEvent);
    }

    public void goToLogin(){
        if (TextUtils.isEmpty(userName.get())){
            ToastUtils.showShort(getApplication().getString(R.string.user_input_hint));
            return;
        }
        if (TextUtils.isEmpty(userPassWord.get())){
            ToastUtils.showShort(getApplication().getString(R.string.password_input_hint));
            return;
        }
        Req_login login = new Req_login(userName.get(), MD5Util.md5(userPassWord.get()),"8888");
        postShowInitLoadViewEvent(true);
        mNetRegister.login(login)
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Observer<LoginResponse>() {
            @Override
            public void onSubscribe(Disposable d) {
                accept(d);
            }
            @Override
            public void onNext(LoginResponse loginResponse) {
                postShowCurrentEvent(true);
                if (loginResponse.retCode == 1){
                    mLoginInfoBean = loginResponse.getLoginInfo();
                    storeUserNameAndPassWord(isRememberPwAndName.get());
                    MmkvStore.getInstance().getMmkv().encode(StoreKey.Module_Login.Token,loginResponse.getToken());

                    getGoPlatformEvent().call();
                }
            }

            @Override
            public void onError(Throwable e) { }

            @Override
            public void onComplete() { }
        });
    }

    public void storeUserNameAndPassWord(boolean flag){
        if (flag){
            MmkvStore.getInstance().getMmkv().encode(StoreKey.Module_Login.User_Name,userName.get());
            MmkvStore.getInstance().getMmkv().encode(StoreKey.Module_Login.Pass_Word,userPassWord.get());
        }
        MmkvStore.getInstance().getMmkv().encode(StoreKey.Module_Login.Save_Name_Password,flag);
        MmkvStore.getInstance().saveClassInfo(StoreKey.Module_Login.Login_Info,mLoginInfoBean);
    }

}
